package com.itsm.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Key;
import java.util.Date;

/**
 * Created by ThinkPad on 2017/6/17.
 */
@Component
public class JwtUtil {
	public static String sercetKey = "mingtianhenganghao";
	public final static long keeptime = 30*60*1000;//30分钟

	/*
	 * @Value("${token.sercetKey}") public static String sercetKey;
	 * 
	 * @Value("${token.keeptime:30000}") public static long keeptime;
	 */

	public static String generToken(String id, String issuer, String subject) {
		long ttlMillis = keeptime;
		SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
		long nowMillis = System.currentTimeMillis();
		Date now = new Date(nowMillis);
		byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(sercetKey);
		Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());

		JwtBuilder builder = Jwts.builder().setId(id).setIssuedAt(now);
		if (subject != null) {
			builder.setSubject(subject);
		}
		if (issuer != null) {
			builder.setIssuer(issuer);
		}
		builder.signWith(signatureAlgorithm, signingKey);

		if (ttlMillis >= 0) {
			long expMillis = nowMillis + ttlMillis;
			Date exp = new Date(expMillis);
			builder.setExpiration(exp);
		}
		return builder.compact();
	}

	public String updateToken(String token) {
		try {
			Claims claims = verifyToken(token);
			String id = claims.getId();
			String subject = claims.getSubject();
			String issuer = claims.getIssuer();
			Date date = claims.getExpiration();
			return generToken(id, issuer, subject);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return "0";
	}

	public String updateTokenBase64Code(String token) {
		BASE64Encoder base64Encoder = new BASE64Encoder();
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			token = new String(decoder.decodeBuffer(token), "utf-8");
			Claims claims = verifyToken(token);
			String id = claims.getId();
			String subject = claims.getSubject();
			String issuer = claims.getIssuer();
			Date date = claims.getExpiration();
			String newToken = generToken(id, issuer, subject);
			return base64Encoder.encode(newToken.getBytes());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return "0";
	}

	public static Claims verifyToken(String token) {
		Claims claims = Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary(sercetKey))
				.parseClaimsJws(token).getBody();
		return claims;
	}

	public static void main(String[] args) {
			String token1 = generToken("jason", "issuer", "subject");
			String token2 = generToken("jason1", null, null);
			Claims claims = verifyToken(token1);
			String id = claims.getId();
			String subject = claims.getSubject();
			String issuer = claims.getIssuer();
			Date date = claims.getExpiration();
			System.out.println("token1====>"+token1);
			System.out.println("id====>"+id);
			System.out.println("issuer====>"+issuer);
			System.out.println("date====>"+new DateTime(date).toString("yyyy-MM-dd HH:mm:ss"));
			System.out.println("subject====>"+subject);
			System.out.println(StringUtils.equals(token1, token2));
			
	}

}